//
//  HomeCustomNavigationBarView.m
//  Btpin
//
//  Created by Vic on 2019/3/16.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "HomeCustomNavigationBarView.h"
#import "CustomSearchEntranceView.h"

#define CityLblWidth (50*Scale)

@interface HomeCustomNavigationBarView ()<CustomSearchEntranceViewDelegate>
{
    UIView *bgView;
    UIButton *filterBtn;
//    UILabel *cityLabel;
//    UIImageView *cityImageView;
//    UIButton *cityButton;
    CustomSearchEntranceView *CSEView;
    
    UIButton *QRBtn;
}
@end

@implementation HomeCustomNavigationBarView

- (instancetype)initWithFrame:(CGRect)frame target:(id)target {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
        self.delegate = target;
    }
    return self;
}
- (void)initUI {
    bgView = [UIToolsControl initViewWithFrame:self.bounds backgroundColor:COLOR_OF_WHITE];
    [self addSubview:bgView];
    
    /*
    cityLabel = [UIToolsControl initLabelWithFrame:CGRectMake(0, HeightOfStatusBar, CityLblWidth, 44) text:@"全国" textColor:COLOR_OF_WHITE font:Font_15];
    cityLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:cityLabel];
    cityImageView = [UIToolsControl initImageViewWithFrame:CGRectMake(EndX(cityLabel)+3*Scale, HeightOfStatusBar+19, 12, 6) imageName:@"city_icon"];
    [self addSubview:cityImageView];
    cityButton = [UIToolsControl initButtonWithFrame:CGRectMake(0, HeightOfStatusBar, ViewWidth(cityLabel)+10*Scale, 44) imageName:nil target:self action:@selector(cityButtonTap) title:nil titleColor:nil];
    [self addSubview:cityButton];
    */
    
    QRBtn = [UIToolsControl initButtonWithFrame:CGRectMake(ScalePS(15), HeightOfStatusBar, 44, 44) imageName:@"qr_code_w" target:self action:@selector(QRBtnTap) title:nil titleColor:nil];
    [self addSubview:QRBtn];
    
    NSString *placeholer = SearchJobCompanyPlaceholderText;
    if (UD_GetOForK(isUserRole) && [UD_GetOForK(isUserRole) isEqualToString:@"1"]) {
        placeholer = SearchWorkerPlaceholderText;
    }
    CGFloat searchW = WIDTH-50*Scale-88;
    CSEView = [[CustomSearchEntranceView alloc] initWithFrame:CGRectMake(EndX(QRBtn)+10*Scale, HeightOfStatusBar+7, searchW, 30) backgroundColor:COLOR_OF_WHITE textColor:THEMECOLOR_OF_GRAY placeholer:placeholer target:self];
    [self addSubview:CSEView];
    
    filterBtn = [UIToolsControl initButtonWithFrame:CGRectMake(EndX(CSEView)+10*Scale, HeightOfStatusBar, 44, 44) imageName:@"filter_icon" target:self action:@selector(filterBtnTap) title:nil titleColor:nil];
    [self addSubview:filterBtn];
}
- (void)filterBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeCustomNavigationBarViewFilter)]) {
        [self.delegate homeCustomNavigationBarViewFilter];
    }
}
- (void)QRBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeCustomNavigationBarViewQRCode)]) {
        [self.delegate homeCustomNavigationBarViewQRCode];
    }
}
- (void)customSearchEntranceViewSearchTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeCustomNavigationBarViewSearch)]) {
        [self.delegate homeCustomNavigationBarViewSearch];
    }
}
- (void)cityButtonTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeCustomNavigationBarViewCity)]) {
        [self.delegate homeCustomNavigationBarViewCity];
    }
}
- (void)setOffsetY:(CGFloat)offsetY {
    if (offsetY <= 0) {
        bgView.alpha = 0;
//        cityLabel.textColor = COLOR_OF_WHITE;
//        cityImageView.image = [UIImage imageNamed:@"city_icon"];
        [QRBtn setImage:[UIImage imageNamed:@"qr_code_b"] forState:UIControlStateNormal];
        CSEView.bgView.backgroundColor = COLOR_OF_WHITE;
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    } else if (offsetY > 0 && offsetY < HeightOfStatusBarAndNavigationBar) {
        bgView.alpha = offsetY / HeightOfStatusBarAndNavigationBar;
//        cityLabel.textColor = THEMECOLOR_OF_TEXT_cccccc;
//        cityImageView.image = [UIImage imageNamed:@"city_icon"];
        CSEView.bgView.backgroundColor = THEMECOLOR_OF_WHITE;
        [QRBtn setImage:[UIImage imageNamed:@"qr_code_b"] forState:UIControlStateNormal];
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    } else if (offsetY >= HeightOfStatusBarAndNavigationBar ) {
        bgView.alpha = 1;
//        cityLabel.textColor = THEMECOLOR_OF_TEXT_9a9a9a;
//        cityImageView.image = [UIImage imageNamed:@"city_icon_gary"];
        CSEView.bgView.backgroundColor = THEMECOLOR_OF_GRAY;
        [QRBtn setImage:[UIImage imageNamed:@"qr_code_b"] forState:UIControlStateNormal];
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    }
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)setCity:(NSString *)city {
    /*
    cityLabel.text = city;
    CGFloat cityW = [UIToolsControl textWidth:city size:CGSizeMake(MAXFLOAT, 44) font:Font_15]+10*Scale;
    cityLabel.frame = CGRectMake(0, HeightOfStatusBar, (cityW>CityLblWidth)?cityW:CityLblWidth, 44);
    
    cityImageView.frame = CGRectMake(EndX(cityLabel)+3*Scale, HeightOfStatusBar+19, 12, 6);
    cityButton.frame = CGRectMake(0, HeightOfStatusBar, ViewWidth(cityLabel)+10*Scale, 44);
    CGFloat searchW = WIDTH-EndX(cityButton)-30*Scale-45;
    CSEView.frame = CGRectMake(EndX(cityImageView)+10*Scale, HeightOfStatusBar+7, searchW, 30);
    [CSEView resetFrame];
    */
}

@end
